//
//  WXPayClient.m
//
//  Created by Northwan on 3/22/14.
//  Copyright (c) 2014 Northwan. All rights reserved.
//

#import "WXPayClient.h"
#import "payRequsestHandler.h"

@interface WXPayClient ()

@property (nonatomic, copy) NSString *timeStamp;
@property (nonatomic, copy) NSString *nonceStr;
@property (nonatomic, copy) NSString *traceId;

//订单参数
@property (nonatomic, copy) NSString *orderNum;
@property (nonatomic, copy) NSString *prodName;
@property (nonatomic, copy) NSString *money;
@property (nonatomic, copy) NSString *notify_url;
@property(nonatomic,weak)UIView *container;
@end

@implementation WXPayClient

#pragma mark - Public
-(void)pay:(NSString *)orderNum prodName:(NSString *)prodName money:(NSString *)money notifyUrl:(NSString *)notifyUrl container:(UIView *)container
{
    self.orderNum = orderNum;
    self.prodName = prodName;
    self.money = money;
    self.notify_url = notifyUrl;
    self.container = container;
    //创建支付签名对象
    payRequsestHandler *req = [[payRequsestHandler alloc] init];
    //初始化支付签名对象
    [req init:kWxAppId mch_id:kWxPartnerID];
    //设置密钥
    [req setKey:kWxPartnerKey];
    
    //获取到实际调起微信支付的参数后，在app端调起支付
    NSMutableDictionary *dict = [req sendPay:orderNum prodName:prodName money:money notifyUrl:notifyUrl];
    
    if(dict == nil){
        //错误提示
        NSString *debug = [req getDebugifo];
        NSLog(@"%@\n\n",debug);
    }else{
        NSLog(@"%@\n\n",[req getDebugifo]);
        //[self alert:@"确认" msg:@"下单成功，点击OK后调起支付！"];
        NSMutableString *stamp  = [dict objectForKey:@"timestamp"];
        //调起微信支付
        PayReq* req             = [[PayReq alloc] init];
        req.openID              = [dict objectForKey:@"appid"];
        req.partnerId           = [dict objectForKey:@"partnerid"];
        req.prepayId            = [dict objectForKey:@"prepayid"];
        req.nonceStr            = [dict objectForKey:@"noncestr"];
        req.timeStamp           = stamp.intValue;
        req.package             = [dict objectForKey:@"package"];
        req.sign                = [dict objectForKey:@"sign"];
        
        [WXApi sendReq:req];
    }
}

@end
